//
//  SceneryDetailViewController.m
//  TravelDiary
//
//  Created by lanou3g on 15/11/14.
//  Copyright © 2015年 YDB MAC. All rights reserved.
//

#import "SceneryDetailViewController.h"
#import <AFNetworking.h>
#import "SceneryImg.h"
#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>

#import "UMSocial.h"


@interface SceneryDetailViewController ()<UIScrollViewDelegate>


@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSString *descriptionStr;

@property(nonatomic,retain)UIImageView *imageView;

@property (nonatomic, strong) AFHTTPSessionManager *manager;

@end

@implementation SceneryDetailViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadNavaBar];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    //请求数据
    _manager=[AFHTTPSessionManager manager];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [self requestData];
    
//    [self loadImageView];
}


- (void)loadNavaBar{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"iconfont-share-sc"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"iconfont-share-sc"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 25, 25);
    [button addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
}
#pragma mark---分享
- (void)shareClick:(UIButton *)sender{
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"564c5b0267e58eed4f0046b2" shareText:@"欢迎进入伴旅" shareImage:[UIImage imageNamed:@"travel_logo"] shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToQzone,UMShareToRenren,UMShareToTwitter, nil] delegate:nil];
}

#pragma mark --数据请求
- (void)requestData{
    self.dataArray = [NSMutableArray array];
    if (_detailUrl==nil) {
        return;
    }
    
    [_manager GET:_detailUrl parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([responseObject isKindOfClass:[NSData class]]) {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            if (dict) {
                self.descriptionStr = dict[@"description"];
                NSArray *array = dict[@"img"];
                for (NSDictionary *dic in array) {
                    [self.dataArray addObject:dic[@"picUrl"]];
                }
            }
        }
        [self loadScrollView];
        [self loadImageView];
        [self loadImageButton];
        [self loadDetailLable];
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}
- (void)loadScrollView{
    //初始化UIScrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height/3)];
    
    
    //是否整屏翻动
    self.scrollView.pagingEnabled = YES;
    //边缘弹动效果
    self.scrollView.bounces = NO;
    //添加到self.view上
    [self.view addSubview:self.scrollView];
    //指定代理人
    self.scrollView.delegate = self;
}
-(void)loadImageView{
    
    for(int i = 0; i < self.dataArray.count; i++)
    {
        //初始化一个UIImageView
        self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height/3)];
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.dataArray[i]] placeholderImage:nil];
        //把UIImageView添加到UIScrollView中 ，subView是一个数组，存的是View的所有子视图
        [self.scrollView addSubview:self.imageView];
        
        
        
    }
    //确保数据拿到之后 才去设置UIScrollView的contentSize
    self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(_scrollView.frame) * self.dataArray.count, self.view.frame.size.height/3);
}

- (void)loadImageButton{
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/3+64, self.view.frame.size.width, 70)];
    scroll.backgroundColor = [UIColor whiteColor];
    
    [scroll setShowsHorizontalScrollIndicator:NO];
    

    [self.view addSubview:scroll];
    
    CGFloat w = 100;
    
    for (int i = 0; i< self.dataArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 10;
        button.frame = CGRectMake(i*w+5*i, 5, w, 60);
        
        [button setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        NSLog(@"%@",self.dataArray[i]);
        
        [button sd_setImageWithURL:self.dataArray[i] forState:UIControlStateNormal];
        
        [scroll addSubview:button];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
    }
    scroll.contentSize = CGSizeMake((w+5)*self.dataArray.count+5, 60);
}

- (void)buttonClick:(UIButton *)sender{
    
    [self.scrollView setContentOffset:CGPointMake(self.view.frame.size.width *[sender.titleLabel.text intValue], 0)];

}

- (void)loadDetailLable{
    
    UIImageView *lineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/3+64+70, self.view.frame.size.width, 35)];
    lineImageView.image = [UIImage imageNamed:@"jingqumiaoshu"];
    [self.view addSubview:lineImageView];
    
    
    UIScrollView *scrollLabel = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/3+64+70+35, self.view.frame.size.width, self.view.frame.size.height - 30-(self.view.frame.size.height/3+64+70+35))];
    [self.view addSubview:scrollLabel];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, self.view.frame.size.width-20, CGRectGetHeight(scrollLabel.frame)-20)];
    label.backgroundColor = [UIColor whiteColor];
    [scrollLabel addSubview:label];
    label.font = [UIFont fontWithName:@"STHeiti TC" size:16];
    label.textColor = [UIColor grayColor];
    
    label.text = [NSString stringWithFormat:@"     %@",self.descriptionStr];
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat h = [self stringHeighWithString:self.descriptionStr fontSize:16 contentSize:CGSizeMake(self.view.frame.size.width , 10000)];
    scrollLabel.contentSize = CGSizeMake(self.view.frame.size.width-20, h);
    
}

- (CGFloat)stringHeighWithString:(NSString *)str fontSize:(CGFloat)fontSize contentSize:(CGSize)size
{
    //第一个参数，代表最大的范围
    //第二个参数，代表的是 是否考虑字体，是否考虑字号
    //第三个参数，代表的是使用什么字体什么字号
    //第四个参数，用不到，所以基本上是nil
    CGRect stringRect = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]} context:nil];
    
    return stringRect.size.height;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
